package com.lyc.shopping_mall.controller;

import com.lyc.shopping_mall.dto.ProductWithCategoryName;
import com.lyc.shopping_mall.result.Result;
import com.lyc.shopping_mall.pojo.Product;
import com.lyc.shopping_mall.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/home") // 统一添加路径前缀
public class HomeController {

    @Autowired
    private ProductService productService;

    @RequestMapping("/getAllProduct")
    public Result<List<Product>> getAllProduct() {
        List<Product> productList = productService.getAllProduct();
        // 判断数据表是否为空
        if (productList == null || productList.isEmpty()) {
            return Result.error("产品列表为空");
        }
        return Result.success(productList, "获取产品列表成功");
    }

    @RequestMapping("/getAdProduct")
    public Result<List<Product>> getAdProduct() {
        List<Product> productList = productService.getAdProduct();
        // 判断数据表是否为空
        if (productList == null || productList.isEmpty()) {
            return Result.error("广告产品列表为空");
        }
        return Result.success(productList, "获取广告产品列表成功");
    }


    @RequestMapping("/getProductByCategory")
    public Result<List<ProductWithCategoryName>> getProductByCategory(String category) {
        List<ProductWithCategoryName> productList = productService.getProductByCategory(category);
        // 判断数据表是否为空
        if (productList == null || productList.isEmpty()) {
            return Result.error(category+"产品列表为空");
        }
        return Result.success(productList, "获取"+category+"产品列表成功");
    }

}



